﻿

/*
* Project: Sherp
* Copyright(c)
* Version: 1.0.0 
* Author: Caijianfeng <sharewind@gmail.com> 
* DateTime: 2011年5月13日 星期五 晚上 20:31:59
*/



package com.cse.sherp.action.xt;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.Sort;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;
import org.tulip.DaoException;

import com.cse.sherp.Constants;
import com.cse.sherp.action.BaseAct;
import com.cse.sherp.business.xt.Tom;
import com.cse.sherp.business.xt.TomDao;
import com.cse.sherp.form.xt.TomForm;

import com.cse.sherp.web.ec.ECConstants;

/**
 * 客户Action
 */
public class TomAct extends BaseAct {
	
	@Override
	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - start"); //$NON-NLS-1$
		TomForm tomForm = (TomForm) form;
		if (null != tomForm) {
		 				
			// 新增
			if (Constants.ACT_ADD.equals(tomForm.getAct())) {
				if (isTokenValid(request)) {
					addTom(tomForm);
					setFormNull(tomForm);
				} else {
					setFormNull(tomForm);
					tomForm.setMessages("请不要重复提交!");
				}
			}

			// 删除
			else if (Constants.ACT_DEL.equals(tomForm
					.getAct())) {
				delTom(tomForm);
				setFormNull(tomForm);
			}
			// 编辑
			else if (Constants.ACT_EDIT.equals(tomForm
					.getAct())) {
				editTom(tomForm);
				tomForm.setAct(Constants.ACT_UPDATE);
			}
			// 编辑后保存--更新
			else if (Constants.ACT_UPDATE.equals(tomForm
					.getAct())) {
				if (isTokenValid(request)) {
					updateTom(tomForm);
					//setFormNull(tomForm);
				} else {
					setFormNull(tomForm);
					tomForm.setMessages("请不要重复提交!");
				}

			}
			//默认显示
			else {
				setFormNull(tomForm);
			}

		}

		// 显示列表
		displayTable(request);
		
		ActionForward returnActionForward = mapping.findForward("success");

		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - end"); 
		return returnActionForward;
	}
	


	/**
	 * 显示客户列表
	 */
	private void displayTable(HttpServletRequest request) throws DaoException {
		TomDao dao = new TomDao();
		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		Limit limit = new TableLimit(limitFactory);
		int totalRows = dao.getTotalToms();
		limit.setRowAttributes(totalRows,
				ECConstants.PAGE_DEFAULT_ROWS_DISPALAY);

		Sort sort = limit.getSort();
		String prop = sort.getAlias();
		String sortOrder = sort.getSortOrder();
		List toms = dao.getToms(prop, sortOrder, limit.getCurrentRowsDisplayed(), limit.getRowStart());
		request.setAttribute("totalRows", new Integer(totalRows));
		request.setAttribute("Toms", toms);
	}

    /**
	 * 更新客户
	 */
	private void updateTom(TomForm tomForm) {
		Tom tom = new Tom();
		try {
			BeanUtils.copyProperties(tom, tomForm);
			new TomDao().updateTom(tom);
		} catch (Exception e) {
			tomForm.setMessages("更新客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 编辑客户
	 */
	private void editTom(TomForm tomForm) {
		try {
			Tom obj = new TomDao().getTomByID(tomForm.getCustId());
			
	        tomForm.setCustId(obj.getCustId());
	        tomForm.setCustName(obj.getCustName());
	        tomForm.setCustTypeId(obj.getCustTypeId());
	        tomForm.setCustAddr(obj.getCustAddr());
	        tomForm.setCustPhone(obj.getCustPhone());
	        tomForm.setCustTel(obj.getCustTel());
	        tomForm.setCustContName(obj.getCustContName());
	        tomForm.setCustStat(obj.getCustStat());
	        tomForm.setCustArea(obj.getCustArea());
	        tomForm.setCustFax(obj.getCustFax());
	        tomForm.setCustMailBox(obj.getCustMailBox());
	        tomForm.setCustEmail(obj.getCustEmail());
	        tomForm.setCustCoun(obj.getCustCoun());
	        tomForm.setCustContJob(obj.getCustContJob());
	        tomForm.setCustCompDesc(obj.getCustCompDesc());
	        tomForm.setCustBank(obj.getCustBank());
	        tomForm.setCustBankAcco(obj.getCustBankAcco());
	        tomForm.setCustSwDjh(obj.getCustSwDjh());
	        tomForm.setCustSfCredMana(obj.getCustSfCredMana());
	        tomForm.setCustSaleMode(obj.getCustSaleMode());
	        tomForm.setCustIncrRate(obj.getCustIncrRate());
	        tomForm.setCustCity(obj.getCustCity());
	        tomForm.setCustProv(obj.getCustProv());
	        tomForm.setCustHomePage(obj.getCustHomePage());
	        tomForm.setCurrId(obj.getCurrId());
	        tomForm.setCustCurrName(obj.getCustCurrName());
	        tomForm.setCustPaymType(obj.getCustPaymType());
	        tomForm.setCustCorpRepr(obj.getCustCorpRepr());
	        tomForm.setCustCustCode(obj.getCustCustCode());
	        tomForm.setCustDeseExpeTaxaCode(obj.getCustDeseExpeTaxaCode());
	        tomForm.setCustAdvaAccoCode(obj.getCustAdvaAccoCode());
	        tomForm.setCustPayExpeTaxaCode(obj.getCustPayExpeTaxaCode());
	        tomForm.setCustPrefPoli(obj.getCustPrefPoli());
	        tomForm.setCustBranPassDept(obj.getCustBranPassDept());
	        tomForm.setCustSpecSale(obj.getCustSpecSale());
	        tomForm.setCustLastTradDate(obj.getCustLastTradDate());
	        tomForm.setCustLastTradMone(obj.getCustLastTradMone());
	        tomForm.setCustLastDeseSkDate(obj.getCustLastDeseSkDate());
	        tomForm.setCustLastDeseMone(obj.getCustLastDeseMone());
	        tomForm.setCustMaxTradMone(obj.getCustMaxTradMone());
	        tomForm.setCustMinDeseRate(obj.getCustMinDeseRate());
	        tomForm.setCustMinSubsRate(obj.getCustMinSubsRate());
	        tomForm.setCustCzLevel(obj.getCustCzLevel());
	        tomForm.setCustAccoTotal(obj.getCustAccoTotal());
	        tomForm.setCustPaymTotal(obj.getCustPaymTotal());
	        tomForm.setCustForgTotal(obj.getCustForgTotal());
	        tomForm.setCustCred(obj.getCustCred());
	        tomForm.setCustSpecKefu(obj.getCustSpecKefu());
	        tomForm.setCustSfCwInit(obj.getCustSfCwInit());
						
		} catch (DaoException e) {
			tomForm.setMessages("查询客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 初始化（置空）客户
	 */
	private void setFormNull(TomForm tomForm) {
		//tomForm.setAct(Constants.ACT_ADD);
		
	    tomForm.setCustId(0);
	    tomForm.setCustName(null);
	    tomForm.setCustTypeId(0);
	    tomForm.setCustAddr(null);
	    tomForm.setCustPhone(null);
	    tomForm.setCustTel(null);
	    tomForm.setCustContName(null);
	    tomForm.setCustStat(0);
	    tomForm.setCustArea(null);
	    tomForm.setCustFax(null);
	    tomForm.setCustMailBox(null);
	    tomForm.setCustEmail(null);
	    tomForm.setCustCoun(null);
	    tomForm.setCustContJob(null);
	    tomForm.setCustCompDesc(null);
	    tomForm.setCustBank(null);
	    tomForm.setCustBankAcco(null);
	    tomForm.setCustSwDjh(null);
	    tomForm.setCustSfCredMana(0);
	    tomForm.setCustSaleMode(null);
	    tomForm.setCustIncrRate(0.0);
	    tomForm.setCustCity(null);
	    tomForm.setCustProv(null);
	    tomForm.setCustHomePage(null);
	    tomForm.setCurrId(0);
	    tomForm.setCustCurrName(null);
	    tomForm.setCustPaymType(0);
	    tomForm.setCustCorpRepr(null);
	    tomForm.setCustCustCode(null);
	    tomForm.setCustDeseExpeTaxaCode(null);
	    tomForm.setCustAdvaAccoCode(null);
	    tomForm.setCustPayExpeTaxaCode(null);
	    tomForm.setCustPrefPoli(null);
	    tomForm.setCustBranPassDept(0);
	    tomForm.setCustSpecSale(0);
	    tomForm.setCustLastTradDate(0);
	    tomForm.setCustLastTradMone(0.0);
	    tomForm.setCustLastDeseSkDate(0);
	    tomForm.setCustLastDeseMone(0.0);
	    tomForm.setCustMaxTradMone(0.0);
	    tomForm.setCustMinDeseRate(0.0);
	    tomForm.setCustMinSubsRate(0.0);
	    tomForm.setCustCzLevel(0);
	    tomForm.setCustAccoTotal(0.0);
	    tomForm.setCustPaymTotal(0.0);
	    tomForm.setCustForgTotal(0.0);
	    tomForm.setCustCred(0.0);
	    tomForm.setCustSpecKefu(0);
	    tomForm.setCustSfCwInit(0);
		
		tomForm.setMessages(null);
	}

	/**
	 * 删除客户
	 */
	private void delTom(TomForm tomForm) {
		Tom tom = new Tom();
		try {
			BeanUtils.copyProperties(tom, tomForm);
			new TomDao().delTom(tom);
		} catch (Exception e) {
			tomForm.setMessages("删除客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 添加客户
	 */
	private void addTom(TomForm tomForm){
		Tom tom = new Tom();
		try {
			BeanUtils.copyProperties(tom, tomForm);
			new TomDao().addTom(tom);
		} catch (Exception e) {
			tomForm.setMessages("添加客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}


}